package com.example.springbootmybatisplusintegrat.user.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springbootmybatisplusintegrat.config.RequestResponseLoggingInterceptor;
import com.example.springbootmybatisplusintegrat.config.tencent.TLSSigAPIv2;
import com.example.springbootmybatisplusintegrat.user.bean.Importmsg2;
import com.example.springbootmybatisplusintegrat.user.mapper.Importmsg2Mapper;
import com.example.springbootmybatisplusintegrat.user.service.Importmsg2Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author mirai
 * @description 针对表【importmsg2】的数据库操作Service实现
 * @createDate 2024-04-28 19:40:24
 */
@Service
@Slf4j
public class Importmsg2ServiceImpl extends ServiceImpl<Importmsg2Mapper, Importmsg2>
        implements Importmsg2Service {

    private final Importmsg2Mapper importmsg2Mapper;


    private static final int EXPIRETIME = 604800;
    private final String tencentKey;
    private final String endPoint;
    private final Long sdkId;
    private final String userIdAdminTencent;

    public Importmsg2ServiceImpl(@Value("${tencent.iam.secret.key}") String tencentKey,
                                 @Value("${tencent.iam.access.endpoint}") String endPoint,
                                 @Value("${tencent.iam.access.sdk.id}") Long sdkId,
                                 @Value("${tencent.iam.access.user.id}") String userIdAdminTencent,
                                 Importmsg2Mapper importmsg2Mapper) {
        this.tencentKey = tencentKey;
        this.endPoint = endPoint;
        this.sdkId = sdkId;
        this.userIdAdminTencent = userIdAdminTencent;
        this.importmsg2Mapper = importmsg2Mapper;
    }


    @Override
    public void importmsg() {

    }


    private ResponseEntity<String> setEndPoint(List<String> accountIds) {
        log.info("Start accountImport: {}", accountIds);
        String fullUri = buildUrl();
        RestTemplate restTemplate = buildRestTemplate();
        HttpEntity<String> entity = buildEntityDismissGroup(accountIds);
        try {
            return restTemplate.exchange(fullUri, HttpMethod.POST, entity, String.class);
        } catch (Exception e) {
            log.error("ERROR in dismissGroup : {}", e.getMessage());
            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    private String buildUrl() {
        String url = endPoint + "/v4/openim/importmsg";
        UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromUriString(url).queryParam("sdkappid", sdkId).queryParam("identifier", userIdAdminTencent).queryParam("usersig", getUserSig(userIdAdminTencent)).queryParam("random", getRandomNumber()).queryParam("contenttype", "json");
        return urlBuilder.toUriString();
    }

    private HttpEntity<String> buildEntityDismissGroup(List<String> accountIds) {
//        MultiAccountImportReqDto multiAccountImportReqDto = new MultiAccountImportReqDto().setAccounts(accountIds);
//        String requestBody = new Gson().toJson(multiAccountImportReqDto);
        HttpHeaders headers = new HttpHeaders();
        return new HttpEntity<>(null, headers);
    }

    private String getUserSig(String userId) {
        TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(sdkId, tencentKey);
        return tlsSigAPIv2.genUserSig(userId, EXPIRETIME);
    }

    private static int getRandomNumber() {
        Random rand = new Random();
        int num = rand.nextInt();
        return Math.abs(num);
    }

    private RestTemplate buildRestTemplate() {
        ClientHttpRequestFactory factory = new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory());
        RestTemplate restTemplate = new RestTemplate(factory);
        List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
        if (org.springframework.util.CollectionUtils.isEmpty(interceptors)) {
            interceptors = new ArrayList<>();
        }
        interceptors.add(new RequestResponseLoggingInterceptor());
        restTemplate.setInterceptors(interceptors);
        return restTemplate;
    }


}




